﻿using System;
using System.Collections.Generic;
using System.Linq;
using SellAndBuy.Domain.Core;

namespace SellAndBuy.Domain.Entities
{
    /// <summary>
    /// Item Category
    /// </summary>
    public class ItemCategory : EntityBase<ItemCategory, int>, IAggregateRoot
    {
        private readonly IList<ItemCondition> _availableConditions = new List<ItemCondition>();

        /// <summary>
        /// Name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Get all available coditions of this category
        /// </summary>
        public IEnumerable<ItemCondition> AvailableConditions
        {
            get
            {
                return _availableConditions;
            }
        }

        /// <summary>
        /// Get condition
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public ItemCondition GetCondition(string condition)
        {
            if (string.IsNullOrWhiteSpace(condition))
                throw new ArgumentNullException(condition);

            if(!AvailableConditions.Contains(new ItemCondition(condition)))
                throw new InvalidOperationException("This condition does not exists.");

            return AvailableConditions.First(c => c.Name == condition);
        }

        /// <summary>
        /// Add condition
        /// </summary>
        /// <param name="condition"></param>
        public void AddCondition(string condition)
        {
            if (string.IsNullOrWhiteSpace(condition))
                throw new ArgumentNullException("condition");

            if (_availableConditions.Contains(new ItemCondition(condition)))
                throw new ArgumentException("This condition already exists.", "condition");

            _availableConditions.Add(new ItemCondition(condition));
        }

        /// <summary>
        /// Remove condition
        /// </summary>
        /// <param name="condition"></param>
        public void RemoveCondition(string condition)
        {
            if (string.IsNullOrWhiteSpace(condition))
                throw new ArgumentNullException("condition");

            if (!_availableConditions.Contains(new ItemCondition(condition)))
                throw new InvalidOperationException("This condition does not exists.");

            _availableConditions.Remove(new ItemCondition(condition));
        }

    }
}
